if(!isObject(fishControlsBehavior))
{  
   %template = new BehaviorTemplate(fishControlsBehavior);
   %template.friendlyName = "Fish Tutorial Controls";
   %template.behaviorType = "Input";
   %template.description  = "Movement control for our Fish Tutorial";
   %template.addBehaviorField(upKey, "Key to bind to upward movement", keybind, "Up");
   %template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "Down");
   %template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "Left");
   %template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "Right");
   %template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 15.0);
   %template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 30.0);
}
function fishControlsBehavior::onBehaviorAdd(%this)
{  
   if (!isObject(moveMap))
      return;     
  
   // bind our keys on the keyboard  
   moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
   moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
   moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
   moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
   %this.up = 0;   %this.down = 0;   %this.left = 0;   %this.right = 0;
}

function fishControlsBehavior::moveUp(%this, %val)
{  
   %this.up = %val;
   %this.updateMovement();
}

function fishControlsBehavior::moveDown(%this, %val)
{  
   %this.down = %val;
   %this.updateMovement();
}

function fishControlsBehavior::moveLeft(%this, %val)
{  
   %this.left = %val;
   %this.updateMovement();
}

function fishControlsBehavior::moveRight(%this, %val)
{  
   %this.right = %val;  
   %this.updateMovement();
}

function fishControlsBehavior::updateMovement(%this)
{  
   // figure out what speed in what direction we are moving based on key input  
   %this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);  
   %this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);     
  
   // check whether or not we need to flip our direction  
   if((%this.left != %this.right) && (%this.left != %this.owner.getFlipX()))     
      %this.owner.setFlipX(%this.left);
}
